{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3eab4051-27cc-4d53-b9d5-bc4c4b69ba8c",
   "metadata": {},
   "source": [
    "# How to specify test criticality?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ef729d6-5eae-4189-9f12-529ea76817a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "from sklearn import datasets\n",
    "\n",
    "from evidently.test_suite import TestSuite\n",
    "from evidently.tests import *"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4333330d-c83c-44dc-9107-9fc2c933f7b0",
   "metadata": {},
   "source": [
    "## Toy Dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "75d19dbf-ba6c-4c90-b25c-f14772878da1",
   "metadata": {},
   "outputs": [],
   "source": [
    "adult_data = datasets.fetch_openml(name='adult', version=2, as_frame='auto')\n",
    "adult = adult_data.frame\n",
    "\n",
    "adult_ref = adult[~adult.education.isin(['Some-college', 'HS-grad', 'Bachelors'])]\n",
    "adult_cur = adult[adult.education.isin(['Some-college', 'HS-grad', 'Bachelors'])]\n",
    "\n",
    "adult_cur.iloc[:2000, 3:5] = np.nan"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c488a2b-2113-40cc-8212-e0db0afbce2e",
   "metadata": {},
   "source": [
    "## Test suite with default criticality "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "27447dec-f355-40cc-829f-136eae5baabc",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_integrity_column_tests = TestSuite(tests=[\n",
    "    TestColumnNumberOfMissingValues(column_name='education'),\n",
    "    TestColumnShareOfMissingValues(column_name='education'),\n",
    "    TestColumnNumberOfDifferentMissingValues(column_name='education'),\n",
    "    TestColumnAllConstantValues(column_name='education'),\n",
    "    TestColumnAllUniqueValues(column_name='education'),\n",
    "    TestColumnRegExp(column_name='education',reg_exp='^[0..9]'),\n",
    "    TestCategoryShare(column_name='education', category='Some-college', lt=0.5),\n",
    "    TestCategoryShare(column_name='age', category=27., lt=0.5)\n",
    "])\n",
    "\n",
    "data_integrity_column_tests.run(reference_data=adult_ref, current_data=adult_cur)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "517c9f2a-2d5c-4b59-abeb-624bdfbbb119",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_integrity_column_tests.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "664803a0-14d4-4c9a-b8d0-1a1932b0f3e5",
   "metadata": {},
   "source": [
    "## Test suite with custom criticality"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42c679b3-c514-429d-85f7-e51f5091e5b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_integrity_column_tests = TestSuite(tests=[\n",
    "    TestColumnNumberOfMissingValues(column_name='education'),\n",
    "    TestColumnShareOfMissingValues(column_name='education'),\n",
    "    TestColumnNumberOfDifferentMissingValues(column_name='education'),\n",
    "    TestColumnAllConstantValues(column_name='education', is_critical=False),\n",
    "    TestColumnAllUniqueValues(column_name='education', is_critical=False),\n",
    "    TestColumnRegExp(column_name='education',reg_exp='^[0..9]'),\n",
    "    TestCategoryShare(column_name='education', category='Some-college', lt=0.5),\n",
    "    TestCategoryShare(column_name='age', category=27., lt=0.5)\n",
    "])\n",
    "\n",
    "data_integrity_column_tests.run(reference_data=adult_ref, current_data=adult_cur)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "deffbb70-c7b9-411c-9798-7273545b6b4f",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_integrity_column_tests.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
